package timing.ukulele.project.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import timing.ukulele.project.mapper.BusinessMapper;
import timing.ukulele.common.ResponseResult;
import timing.ukulele.persistent.service.BaseService;
import timing.ukulele.project.persistent.Business;
import timing.ukulele.project.persistent.BusinessData;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class BusinessService extends BaseService<BusinessMapper, Business> {
    private final BusinessDataService businessDataService;

    public BusinessService(BusinessDataService businessDataService) {
        this.businessDataService = businessDataService;
    }

    public ResponseResult<String> removeProject(Long id) {
        List<BusinessData> list = businessDataService.getHistoryBusinessDataById(id);
        if (CollectionUtils.isEmpty(list)) {
            removeById(id);
            return ResponseResult.success();
        } else {
            Business business = getById(id);
            if (StringUtils.isEmpty(business.getFlowInstance())) {
                removeById(id);
                businessDataService.removeByIds(list.stream().map(BusinessData::getId).collect(Collectors.toList()));
                return ResponseResult.success();
            }
            return ResponseResult.error("办件已经启动了流程，不可删除");
        }
    }

    public Business getByBusinessCode(String busincessCode) {
        LambdaQueryWrapper<Business> qw = new LambdaQueryWrapper<>();
        qw.eq(Business::getBusinessCode, busincessCode);
        return getBaseMapper().selectOne(qw);
    }

    /**
     * 获取分页
     *
     * @param name    查询名称
     * @param current 当前也页，从1开始
     * @param size    每页大小
     * @return 模板数据
     */
    public IPage<Business> getPage(String name, String starter, String system, int current, int size) {
        Page<Business> page = new Page<>(current, size);
        LambdaQueryWrapper<Business> query = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            query.like(Business::getItemName, name);
        }
        if (!StringUtils.isEmpty(starter)) {
            query.like(Business::getStarter, starter);
        }
        List<Long> systemIds;
        if (!"".equals(system)) {
            String[] split = system.split(",");
            if (!"0".equals(split[0])) {
                systemIds = Arrays.asList(split).stream().map(s -> Long.parseLong(s.trim())).collect(Collectors.toList());
                query.in(Business::getSystemId, systemIds);
            }
        }
        query.orderByDesc(Business::getId);
        IPage<Business> iPage = getBaseMapper().selectPage(page, query);
        return iPage;
    }

}
